package com.bbm.communitysystem.controller;


import cn.hutool.core.bean.BeanUtil;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.Property;
import com.bbm.communitysystem.domain.dto.PropertyDto;
import com.bbm.communitysystem.service.IPropertyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-02-13
 */
@Tag(name = "物业")
@Slf4j
@RestController
@RequestMapping("/property")
@RequiredArgsConstructor
public class PropertyController {

    private final IPropertyService propertyService;

    /**
     * 暂时无vo
     */
    @Operation(summary = "获取物业详情", description = "暂时无vo")
    @GetMapping("/{id}")
    public R getProperty(@PathVariable("id") Integer id) {
        Property byId = propertyService.getById(id);

        return R.ok(propertyService.getById(id));
    }

    @Operation(summary = "查询物业列表， 后期修改")
    @PostMapping("/list")
    public R getPropertyList(@RequestParam("page") Integer page,
                             @RequestParam("size") Integer size) {


        return R.ok(propertyService.list());
    }

    @PostMapping("/")
    public R<String> addProperty(@RequestBody PropertyDto propertyDto) {

        Property property = BeanUtil.copyProperties(propertyDto, Property.class);
        property.setUpdateTime(System.currentTimeMillis() + "");

        boolean save = propertyService.save(property);

        if (!save) {
            return R.error("新增失败");
        }

        return R.ok("新增成功");
    }

    @PutMapping("/")
    public R putProperty(@RequestBody PropertyDto propertyDto) {

        boolean update = propertyService.lambdaUpdate()
                .set(Property::getUpdateBy, propertyDto.getUpdateBy())
                .set(Property::getUpdateTime, System.currentTimeMillis() + "")
                .set(!propertyDto.getPropertyName().isEmpty(), Property::getPropertyName, propertyDto.getPropertyName())
                .set(!propertyDto.getPropertyDescription().isEmpty() ,Property::getPropertyDescription, propertyDto.getPropertyDescription())
                .eq(Property::getId, propertyDto.getPropertyId())
                .update();

        if (!update) {
            return R.error("修改失败");
        }

        return R.ok();
    }

    @DeleteMapping("/{id}")
    public R<String> deleteProperty(@PathVariable("id") Integer id) {
        boolean b = propertyService.removeById(id);
        if (!b) {
            return R.error("删除失败");
        }

        return R.ok("成功");
    }



}
